//
//  HMNewFeatureViewController.swift
//  大深圳微博01
//
//  Created by shenzhenIOS on 16/4/11.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HMNewFeatureViewController: UICollectionViewController {
    
    /// 布局参数
    let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init() {
        super.init(collectionViewLayout: layout)
        
        // 设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(HMNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    // 如果不写,默认是1
//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 0
//    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMNewFeatureCell
    
//        cell.backgroundColor = UIColor.randomColor() // UIColor.random()
        
        // 设置cell的index,告诉他显示的是第几页
        cell.index = indexPath.item
//        cell.startAnimation()
    
        return cell
    }
}

// MARK: - 扩展HMNewFeatureViewController 实现 scrollViewDidEndDecelerating 方法
extension HMNewFeatureViewController {
    // scrollView停止滚动时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        HMPrint("scrollView停止滚动时调用")
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
//        HMPrint("scrollView停止滚动时调用,页码: \(index)")
        // 判断是否是最后一页
        if index == NumberOfNewFeatureItem - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            // 最后一页,让cell开始动画
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HMNewFeatureCell
            cell.startAnimation()
        }
    }
}

// 在一个文件里面写多个类
class HMNewFeatureCell: UICollectionViewCell {
    
    // 当前cell显示的页码
    var index: Int = 0 {
        didSet {
            // 当控制器给cell的index设置值后
            // 拼接图片名称
            let imageName = "new_feature_\(index + 1)"
//            HMPrint("imageName: \(imageName)")
            bkgImageView.image = UIImage(named: imageName)
            
            // 隐藏按钮
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 系统会来调用这个方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加控件
        // 当是cell的时候,view都添加到contentView
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        // 2.添加约束
        // 背景
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    // MARK: - 动画方法
    func startAnimation() {
        // 显示按钮
        startButton.hidden = false
        
        // 将transform的缩放改成0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            self.startButton.transform = CGAffineTransformIdentity  // 清空transform的缩放, 1
            }) { (_) -> Void in
                HMPrint("动画完成了")
              
        }
    }
    
    
    // MARK: - ******************* Btn点击事件 ********************
    
   @objc func DidClickStartBtn() {
    
        let controller = CZTabBarController()
        (UIApplication.sharedApplication().delegate as!AppDelegate).switchViewcontroller(controller)
        
    
    }
    
    // MARK: - 懒加载
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(HMNewFeatureLabelFontSize)
        
        button.sizeToFit()
        
        
        button.addTarget(self, action: #selector(DidClickStartBtn), forControlEvents: .TouchUpInside)
        
        return button
    }()
}